package com.prevpn.controller;

import com.prevpn.common.CommonResponse;
//import com.prevpn.dto.ResourceAssignByNameDTO;
import com.prevpn.dto.TunnelAppDTO;
import com.prevpn.dto.WebAppDTO;
import com.prevpn.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Web 应用管理控制器
 */
@RestController
@RequestMapping("/app")
public class AppController {

    @Autowired
    private AppService appService;

    /**
     * 新增 Web 应用
     * POST /webapp/create
     */
    @PostMapping("/webapp/create")
    public CommonResponse<Map<String, Object>> createWebApp(@RequestBody WebAppDTO webAppDTO) {
        return appService.createWebApp(webAppDTO);
    }

    /**
     * 新增隧道应用
     * POST /tunnelapp/create
     */
    @PostMapping("/tunnelapp/create")
    public CommonResponse<Map<String, Object>> createTunnelApp(@RequestBody TunnelAppDTO tunnelAppDTO) {
        return appService.createTunnelApp(tunnelAppDTO);
    }

    /**
     * 基于应用名称授权（角色、用户、组织架构）
     * POST /role/assignByName
     */
    //@PostMapping("/assignByName")
    //public CommonResponse<Map<String, Object>> assignByName(@RequestBody ResourceAssignByNameDTO dto) {
    //    return appService.assignByName(dto);
    //}

    /**
     * 基于名称查询应用详情
     * GET /app/queryByName?name=xxx
     */
    @GetMapping("/queryByName")
    public CommonResponse<Map<String, Object>> queryAppByName(@RequestParam("name") String name) {
        return appService.queryAppByName(name);
    }

    /**
     * 查询应用列表（支持分页、搜索、分类过滤）
     * GET /app/queryAll
     */
    @GetMapping("/queryAll")
    public CommonResponse<Map<String, Object>> queryAppList(@RequestBody Map<String, Object> body) {
        return appService.queryAppList(body);
    }

    /**
     * 批量删除应用（基于名称或ID）
     */
    @PostMapping("/deleteResource")
    public CommonResponse<Map<String, Object>> deleteAppResource(@RequestBody Map<String, Object> body) {
        return appService.deleteAppResource(body);
    }

    /**
     * 编辑 WEB 应用
     * Path: /app/webapp/update
     * name或 id 二选一
     */
    @PostMapping("/webapp/update")
    public CommonResponse<Map<String, Object>> updateWebApp(@RequestBody Map<String, Object> body) {
        return appService.updateWebApp(body);
    }


    /**
     * 编辑 隧道应用
     * Path: /app/tunnelapp/update
     */
    @PostMapping("/tunnelapp/update")
    public CommonResponse<Map<String, Object>> updateTunnelApp(@RequestBody Map<String, Object> body) {
        return appService.updateTunnelApp(body);
    }
}
